package com.hotel_management_backstage.lin.service;

import com.hotel_management_backstage.lin.dao.SysResidentRoomMapper;
import com.hotel_management_backstage.lin.entity.SysResident;
import com.hotel_management_backstage.lin.entity.SysResidentRoom;
import com.hotel_management_backstage.lin.entity.SysRoom;
import com.hotel_management_backstage.lin.entity.SysRoomStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class SysResidentRoomService {
    @Autowired
    private SysResidentRoomMapper sysResidentRoomMapper;

    @Autowired
    private SysResidentService sysResidentService;

    @Autowired
    private SysRoomService sysRoomService;

    @Transactional
    public void updateRoomStatusByRoomNumber(SysRoomStatus sysRoomStatus){
        SysRoom sysRoom = sysRoomService.selectRoomByRoomNumber(sysRoomStatus.getRoomNumber());
        Integer roomId = sysRoom.getId();
        SysResidentRoom sysResidentRoom = sysResidentRoomMapper.selectByRoomId(roomId);
        Integer residentId = sysResidentRoom.getResidentId();
        SysResident sysResident = sysResidentService.selectById(residentId);
//        sysRoom.setRoomNumber(sysRoomStatus.getRoomNumber());
//        sysRoom.setRoomInformation(sysRoomStatus.getRoomInformation());
//        sysRoom.setRoomIntroduction(sysRoomStatus.getRoomIntroduction());
        sysResident.setIdCard(sysRoomStatus.getIdCard());
        sysResident.setName(sysRoomStatus.getName());
        sysResident.setCheckInTime(sysRoomStatus.getCheckInTime());
        sysResident.setCheckOutTime(sysRoomStatus.getCheckOutTime());
//        sysRoomService.updateRoom(sysRoom);
        sysResidentService.updateResident(sysResident);
    }

    @Transactional
    public void deleteRoomStatusByRoomNumber(int roomNumber){
        SysRoom sysRoom = sysRoomService.selectRoomByRoomNumber(roomNumber);
        sysRoom.setRoomReserved("否");
        sysRoomService.updateRoom(sysRoom);
        Integer roomId = sysRoom.getId();
        SysResidentRoom sysResidentRoom = sysResidentRoomMapper.selectByRoomId(roomId);
        Integer residentId = sysResidentRoom.getResidentId();
//        sysRoomService.deleteRoomById(roomId);
        sysResidentService.deleteResidentById(residentId);
        sysResidentRoomMapper.deleteResidentRoomByResidentId(residentId);
    }

    public SysResidentRoom selectByRoomId(int roomId){
        return sysResidentRoomMapper.selectByRoomId(roomId);
    }

    public List<SysResidentRoom> selectByResidentId(int id){
        return sysResidentRoomMapper.listByResidentId(id);
    }

    @Transactional
    public void insertRoomStatus(SysRoomStatus sysRoomStatus){
        SysResident sysResident = new SysResident();
        sysResident.setIdCard(sysRoomStatus.getIdCard());
        sysResident.setName(sysRoomStatus.getName());
        sysResident.setCheckInTime(sysRoomStatus.getCheckInTime());
        sysResident.setCheckOutTime(sysRoomStatus.getCheckOutTime());
        sysResidentService.insertResident(sysResident);
        Integer residentId = sysResident.getId();
//        SysRoom sysRoom = new SysRoom();
//        sysRoom.setRoomNumber(sysRoomStatus.getRoomNumber());
//        sysRoom.setRoomInformation(sysRoomStatus.getRoomInformation());
//        sysRoom.setRoomIntroduction(sysRoomStatus.getRoomIntroduction());
//        sysRoomService.insertRoom(sysRoom);
        SysRoom sysRoom = sysRoomService.selectRoomByRoomNumber(sysRoomStatus.getRoomNumber());
        sysRoom.setRoomReserved("是");
        sysRoomService.updateRoom(sysRoom);
        Integer roomId = sysRoom.getId();
        SysResidentRoom sysResidentRoom = new SysResidentRoom();
        sysResidentRoom.setResidentId(residentId);
        sysResidentRoom.setRoomId(roomId);
        sysResidentRoomMapper.insertResidentRoom(sysResidentRoom);
    }

    public List<SysRoomStatus> selectAllRoomStatus(){
        List<SysRoomStatus> sysRoomStatuses = new ArrayList<>();
        List<SysResident> sysResidents = sysResidentService.selectAll();
        for(SysResident sysResident : sysResidents ){
            List<SysResidentRoom> sysResidentRooms = sysResidentRoomMapper.listByResidentId(sysResident.getId());
            for(SysResidentRoom sysResidentRoom : sysResidentRooms){
                SysRoom sysRoom = sysRoomService.selectById(sysResidentRoom.getRoomId());
                SysRoomStatus sysRoomStatus = new SysRoomStatus();
                sysRoomStatus.setRoomNumber(sysRoom.getRoomNumber());
                sysRoomStatus.setRoomInformation(sysRoom.getRoomInformation());
                sysRoomStatus.setRoomIntroduction(sysRoom.getRoomIntroduction());
                sysRoomStatus.setName(sysResident.getName());
                sysRoomStatus.setIdCard(sysResident.getIdCard());
                sysRoomStatus.setCheckInTime(sysResident.getCheckInTime());
                sysRoomStatus.setCheckOutTime(sysResident.getCheckOutTime());
                sysRoomStatuses.add(sysRoomStatus);
            }
        }
        return sysRoomStatuses;
    }
}
